import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/index")
public class Index extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.看当前是否登录成功
        HttpSession httpSession = req.getSession(false);
        if (httpSession == null){
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("用户未登录");
            return;
        }
        //登录成功
        String username = (String) httpSession.getAttribute("username");
        Long time = (Long) httpSession.getAttribute("time");
        System.out.println("用户" + username  + "登陆成功");
        resp.setContentType("text/html;charset=utf8");
        String respBody = "欢迎您！" + username + " 您上次登录时间为:" + time;
        resp.getWriter().write(respBody);
    }
}
